##=============================================================================
## Appendix Figure 32
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "sf")

ipak(packages)

##---------
# Load Data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

# Create time series
dates <- tibble(yrmo = seq(as.Date("2022-02-01"),
                           as.Date("2023-11-01"),
                           by = "1 month"))

provs <- ukr_adm1 %>%
  distinct(adm1_en)

adm1_yrmo <- expand_grid(provs, dates)

rm(dates, provs)

# By Oblast
viina_adm1_month <- viina %>% 
  filter(adm1_en != "Avtonomna Respublika Krym" & adm1_en != "Sevastopilska") %>% 
  filter(yrmo < as.Date("2023-03-01")) %>% 
  filter(t_mil_b == 1 & t_loc_b == 1 ) %>% 
  filter(geo_precision == "ADM1" | geo_precision == "ADM2" | 
           geo_precision == "ADM3" | geo_precision == "STREET") %>% 
  group_by(yrmo, adm1_en) %>% 
  tally(., name = "viina_nom") %>% 
  arrange(adm1_en) %>% 
  right_join(., adm1_yrmo, by = c("yrmo", "adm1_en")) %>% 
  arrange(yrmo, adm1_en) %>%
  mutate(viina_nom = ifelse(is.na(viina_nom), 0, viina_nom)) %>% 
  ungroup() %>% 
  group_by(adm1_en) %>% 
  mutate(viina_cum_adm1 = cumsum(viina_nom)) %>% 
  mutate(viina_cum_adm1_pct_change = ((1+viina_cum_adm1) - (1+lag(viina_cum_adm1)))/
           (1+lag(viina_cum_adm1)) * 100) %>% 
  mutate(viina_cum_adm1_pct_change = round(viina_cum_adm1_pct_change, 1)) %>% 
  mutate(adm1_pop_2022 = case_when(adm1_en == "Kyiv City" ~ 2950702,
                                   adm1_en == "Kyivska" ~ 1795542,
                                   adm1_en == "Vinnytska" ~ 1507738,
                                   adm1_en == "Volynska" ~ 1020770,
                                   adm1_en == "Dnipropetrovska" ~ 3093151,
                                   adm1_en == "Donetska" ~ 4056405,
                                   adm1_en == "Zhytomyrska" ~ 1177564,
                                   adm1_en == "Zakarpatska" ~ 1243721,
                                   adm1_en == "Zaporizka" ~ 1636322,
                                   adm1_en == "Ivano-Frankivska" ~ 1350565,
                                   adm1_en == "Kirovohradska" ~ 902275, 
                                   adm1_en == "Luhanska" ~ 2101653,
                                   adm1_en == "Lvivska" ~ 2476113,
                                   adm1_en == "Mykolaivska" ~ 1090492,                                   
                                   adm1_en ==  "Odeska" ~ 2349749,
                                   adm1_en == "Poltavska" ~ 1350564,
                                   adm1_en == "Rivnenska" ~ 1140902,
                                   adm1_en == "Sumska" ~ 1034364,
                                   adm1_en == "Ternopilska" ~ 1020953,
                                   adm1_en == "Kharkivska" ~ 2596250,
                                   adm1_en == "Khersonska" ~ 1000370,
                                   adm1_en == "Khmelnytska" ~ 1227474,
                                   adm1_en == "Cherkaska" ~ 1159200,
                                   adm1_en == "Chernivetska" ~ 889928,
                                   adm1_en == "Chernihivska" ~ 957665,
                                   adm1_en == "Mykolaivska" ~ 1090492)) %>% 
  mutate(viina_cum_per_100k_adm1 = (round((viina_nom/adm1_pop_2022)*100000, 
                                          digits = 2)))

viina_adm1_month %>%
  dplyr::select(adm1_en, yrmo, viina_cum_adm1) %>% 
  filter(adm1_en != "Avtonomna Respublika Krym" & adm1_en != "Sevastopilska") %>% 
  filter(yrmo == as.Date("2022-06-01") | yrmo == as.Date("2023-02-01")) %>% 
  mutate(wave = ifelse(yrmo == "2022-06-01", 0, 1)) %>% 
  mutate(wave = factor(wave, levels = c(0, 1))) %>% 
  dplyr::select(adm1_en, wave, viina_cum_adm1) %>% 
  arrange(adm1_en) %>% 
  mutate(viina_cum_pct_change = ((1+viina_cum_adm1) - (1+lag(viina_cum_adm1)))/(1+lag(viina_cum_adm1)) * 100) %>% 
  mutate(viina_cum_pct_change = round(viina_cum_pct_change, 1)) %>% 
  filter(wave == 1) %>% 
  dplyr::select(adm1_en, viina_cum_pct_change) %>% 
  arrange(viina_cum_pct_change) %>% 
  print(n = 24) 

ggplot(viina_adm1_month, aes(x = yrmo, y = viina_cum_adm1)) +
  facet_wrap(~adm1_en, nrow = 7) +
  geom_area(fill = "gray75", color = "gray10") +
  scale_y_continuous(breaks = c(0, 10000, 20000, 30000),
                     limits = c(0, 30000)) +
  scale_x_date(breaks = "4 month", minor_breaks = "1 month",
               labels = scales::date_format("%m/%y")) +
  labs(x = NULL,
       y = "Count",
       caption = "Source: VIINA (Zhukov 2023)") +
  hrbrthemes::theme_ipsum(axis_title_size = 16,
                          axis_text_size = 14,
                          plot_title_size = 24,
                          caption_size = 16,
                          base_size = 12)

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================